package com.lagou.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {
    private OutputStream outputStream;

    /**
     * 输出字符串
     *
     * @param content 字符串
     */
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    /**
     * 输出静态资源
     *
     * @param path 静态资源相对路径
     */
    public void outputHtml(String path) throws IOException {
        //静态资源绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
        File file = new File(absoluteResourcePath);
        if (file.exists() && file.isFile()) {
            //输出
            StaticResourceUtil.outputStaticResource(new FileInputStream(file), outputStream);
        } else {
            //404
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }
}
